<template>
	<el-dialog
		:title="title"
		:visible.sync="addComShow"
		width="60%"
		:before-close="cancel"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
		top="5vh"
	>
		<el-form label-width="110px" label-position="left">
			<el-form-item label="管理人代码" title="统一社会信用编码" required>
				<el-select
					v-model="managementInfo.managementRegisterNumber"
					filterable
					remote
					@change="registerNumberChange"
					reserve-keyword
					placeholder="请输入关键词"
					:remote-method="remoteMethod"
					:loading="loading"
					style="width: 100%"
				>
					<el-option v-for="(item, index) in compintroductionList" :key="index" :value="item.registernumber">
						<span>{{ item.registernumber }}-{{ item.compName }}</span>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="管理人简称" required>
				<el-input v-model.trim="managementInfo.managementName" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="管理人全称" required>
				<el-input v-model.trim="managementInfo.managementFullName" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="管理人英文名称">
				<el-input v-model.trim="managementInfo.managementEnglishName" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="组织机构代码">
				<el-input v-model.trim="managementInfo.organCode" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="成立日期">
				<el-date-picker
					style="width: 100%"
					type="date"
					v-model="managementInfo.foundDate"
					value-format="yyyyMMdd"
					format="yyyyMMdd"
					:disabled="isDisabled"
				></el-date-picker>
			</el-form-item>
			<el-form-item label="办公地址">
				<el-input v-model.trim="managementInfo.address" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="法人代表">
				<el-input v-model.trim="managementInfo.chairman" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="总经理">
				<el-input v-model.trim="managementInfo.president" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="公司简介">
				<el-input type="textarea" v-model.trim="managementInfo.briefing" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="联系电话">
				<el-input v-model.trim="managementInfo.phone" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="传真">
				<el-input v-model.trim="managementInfo.fax" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
			<el-form-item label="邮箱">
				<el-input v-model.trim="managementInfo.email" type="text" :disabled="isDisabled" @input="refresh"></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button size="small" @click="cancel">取 消</el-button>
			<el-button size="small" type="primary" @click="addConfirm">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { addManagementInfo, editManagementInfo } from '@/api/productApi/mechanism'
import { getCompintroduction } from '@/api/productApi/file'
import { EmptyJudgment } from '@/utils/validate'
export default {
	props: {
		title: {
			type: String,
			default: ''
		},
		addComShow: {
			type: Boolean,
			default: false
		},
		comIsShow: {
			type: Function,
			default: Function
		},
		queryFundManagerList: {
			type: Function,
			default: Function
		}
	},
	data() {
		return {
			managementInfo: {
				managementRegisterNumber: ''
			},
			isDisabled: false,
			compintroductionList: [],
			paging: {
				size: 20, // 显示的条目数
				num: 1 // 当前页数
			},
			sInfoComptype: '18', // 默认查询：万德公司类型(18:基金管理公司)
			loading: false
		}
	},
	mounted() {},
	methods: {
		cancel() {
			this.$emit('comIsShow')
			this.managementInfo = {
				managementRegisterNumber: ''
			}
		},
		remoteMethod(query) {
			this.managementInfo.managementRegisterNumber = query
			this.remoteMethodList(query)
		},
		// 新增或编辑
		addConfirm() {
			if (
				EmptyJudgment(this.managementInfo.managementRegisterNumber) ||
				EmptyJudgment(this.managementInfo.managementName) ||
				EmptyJudgment(this.managementInfo.managementFullName)
			) {
				this.$message({
					type: 'warning',
					message: '请填写必要参数'
				})
				return
			}
			if (this.title === '新增') {
				addManagementInfo({ managementInfo: this.managementInfo }).then(res => {
					if (res.msgCode === 'Y') {
						this.$emit('queryFundManagerList')
						this.cancel()
						this.$message({ message: res.msgDesc, type: 'success' })
					}
				})
			} else if (this.title === '修改') {
				editManagementInfo({ managementId: this.managementInfo.managementId, managementInfo: this.managementInfo }).then(res => {
					if (res.msgCode === 'Y') {
						this.$emit('queryFundManagerList')
						this.cancel()
						this.$message({ message: res.msgDesc, type: 'success' })
					}
				})
			}
		},
		refresh() {
			this.$forceUpdate()
		},
		registerNumberChange() {
			const compInfo = this.compintroductionList.filter(item => item.registernumber === this.managementInfo.managementRegisterNumber)
			if (compInfo.length) {
				// 万德公司ID
				this.$set(this.managementInfo, 'wdComId', compInfo[0].compId)
				// 管理人代码
				this.$set(this.managementInfo, 'managementRegisterNumber', compInfo[0].registernumber)
				// 管理人简称
				this.$set(this.managementInfo, 'managementName', compInfo[0].compSname)
				// 管理人全称
				this.$set(this.managementInfo, 'managementFullName', compInfo[0].compName)
				// 组织机构代码
				this.$set(this.managementInfo, 'organCode', compInfo[0].organCode)
				// 成立日期
				this.$set(this.managementInfo, 'foundDate', compInfo[0].foundDate)
				// 办公地址
				this.$set(this.managementInfo, 'address', compInfo[0].address)
				// 法人代表
				this.$set(this.managementInfo, 'chairman', compInfo[0].chairman)
				// 总经理
				this.$set(this.managementInfo, 'president', compInfo[0].president)
				// 公司简介
				this.$set(this.managementInfo, 'briefing', compInfo[0].briefing)
				// 联系电话
				this.$set(this.managementInfo, 'phone', compInfo[0].phone)
				// 传真
				this.$set(this.managementInfo, 'fax', compInfo[0].fax)
				// 邮箱
				this.$set(this.managementInfo, 'email', compInfo[0].email)
				// 管理人英文名称
				this.$set(this.managementInfo, 'managementEnglishName', compInfo[0].compNameEng)
			}
		},
		edit(obj) {
			this.managementInfo = JSON.parse(obj)
			this.remoteMethodList()
		},
		// 查询：万德公司信息
		remoteMethodList(value) {
			if (value) {
				const params = {}
				// 搜索参数
				params.sInfoComptypes = '18'
				params.compIdOrName = value
				this.loading = true
				getCompintroduction(params, this.paging).then(res => {
					if (res.msgCode === 'Y') {
						// 万德公司信息
						this.compintroductionList = res.data.body.data.body
						this.registerNumberChange()
					}
					this.loading = false
				})
			}
		}
	}
}
</script>
<style lang="scss" scoped >
::v-deep .el-dialog__body {
	margin: -20px 0 -40px 0;
}
</style>
